{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "from point_e.models.download import load_checkpoint\n",
    "from point_e.models.configs import MODEL_CONFIGS, model_from_config\n",
    "from point_e.util.pc_to_mesh import marching_cubes_mesh\n",
    "from point_e.util.plotting import plot_point_cloud\n",
    "from point_e.util.point_cloud import PointCloud"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "\n",
    "print('creating SDF model...')\n",
    "name = 'sdf'\n",
    "model = model_from_config(MODEL_CONFIGS[name], device)\n",
    "model.eval()\n",
    "\n",
    "print('loading SDF model...')\n",
    "model.load_state_dict(load_checkpoint(name, device))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load a point cloud we want to convert into a mesh.\n",
    "pc = PointCloud.load('example_data/pc_corgi.npz')\n",
    "\n",
    "# Plot the point cloud as a sanity check.\n",
    "fig = plot_point_cloud(pc, grid_size=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Produce a mesh (with vertex colors)\n",
    "mesh = marching_cubes_mesh(\n",
    "    pc=pc,\n",
    "    model=model,\n",
    "    batch_size=4096,\n",
    "    grid_size=32, # increase to 128 for resolution used in evals\n",
    "    progress=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write the mesh to a PLY file to import into some other program.\n",
    "with open('mesh.ply', 'wb') as f:\n",
    "    mesh.write_ply(f)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.9 64-bit ('3.9.9')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b270b0f43bc427bcab7703c037711644cc480aac7c1cc8d2940cfaf0b447ee2e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
